﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinkCard.Model;

namespace LinkCard.Dao
{
    public class TagDao
    {
        public int AddDealTag(long dealid, string keyTag)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var tagsObj = db.TagsDeal.Where(t => t.DealId == dealid).ToList();
                if (tagsObj.Count > 0)
                {
                    foreach (var tagsMessage in tagsObj)
                    {
                        db.TagsDeal.DeleteObject(tagsMessage);
                        long a1 = tagsMessage.TagId;
                        var a2 = db.Tags.FirstOrDefault(t => t.TagId == a1);
                        if (a2 != null)
                        {
                            a2.UsedCount--;
                        }
                    }
                    db.SaveChanges();
                }


                //add tags moi
                var tags = keyTag.Split(',').ToList();

                foreach (var tag in tags)
                {
                    var tagString = tag.Trim();
                    var tagId = 0;

                    //kiem tra xem co tagId by string nay chua?
                    var tag1 = db.Tags.FirstOrDefault(t => t.TagName == tagString);
                    if (tag1 != null)
                    {
                        tagId = tag1.TagId;
                        //UsedCount++
                        tag1.UsedCount++;
                        db.SaveChanges();
                    }
                    else
                    {
                        //insert tagName
                        var tag2 = new Tags() { TagName = tagString, UsedCount = 1 };
                        db.Tags.AddObject(tag2);

                        if (db.SaveChanges() > 0)
                        {
                            tagId = tag2.TagId;
                        }

                    }
                    if (tagId > 0)
                        db.TagsDeal.AddObject(new TagsDeal() { DealId = dealid, TagString = tagString, TagId = tagId });
                }
                db.SaveChanges();
            }
            catch (Exception)
            {
                return rs;
                //throw;
            }
            return rs;
        }

        public int AddMessageTag(long messageid, string keyTag)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var tagsObj = db.TagsMessage.Where(t => t.MessageId == messageid).ToList();
                if (tagsObj.Count > 0)
                {
                    foreach (var tagsMessage in tagsObj)
                    {
                        db.TagsMessage.DeleteObject(tagsMessage);
                        long a1 = tagsMessage.TagId;
                        var a2 = db.Tags.FirstOrDefault(t => t.TagId == a1);
                        if (a2 != null)
                        {
                            a2.UsedCount--;
                        }
                    }
                    db.SaveChanges();
                }


                //add tags moi
                var tags = keyTag.Split(',').ToList();

                foreach (var tag in tags)
                {
                    var tagString = tag.Trim();
                    var tagId = 0;

                    //kiem tra xem co tagId by string nay chua?
                    var tag1 = db.Tags.FirstOrDefault(t => t.TagName == tagString);
                    if (tag1 != null)
                    {
                        tagId = tag1.TagId;
                        //UsedCount++
                        tag1.UsedCount++;
                        db.SaveChanges();
                    }
                    else
                    {
                        //insert tagName
                        var tag2 = new Tags() { TagName = tagString, UsedCount = 1 };
                        db.Tags.AddObject(tag2);

                        if (db.SaveChanges() > 0)
                        {
                            tagId = tag2.TagId;
                        }

                    }
                    if (tagId > 0)
                        db.TagsMessage.AddObject(new TagsMessage() { MessageId = messageid, TagString = tagString, TagId = tagId });
                }
                db.SaveChanges();
            }
            catch (Exception)
            {
                return rs;
                //throw;
            }
            return rs;
        }

        public int AddSupplierTag(long supplierid, string keyTag)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var tagsObj = db.TagsSupplier.Where(t => t.SupplierId == supplierid).ToList();
                if (tagsObj.Count > 0)
                {
                    foreach (var tagsMessage in tagsObj)
                    {
                        db.TagsSupplier.DeleteObject(tagsMessage);
                        long a1 = tagsMessage.TagId;
                        var a2 = db.Tags.FirstOrDefault(t => t.TagId == a1);
                        if (a2 != null)
                        {
                            a2.UsedCount--;
                        }
                    }
                    db.SaveChanges();
                }


                //add tags moi
                var tags = keyTag.Split(',').ToList();

                foreach (var tag in tags)
                {
                    var tagString = tag.Trim();
                    var tagId = 0;

                    //kiem tra xem co tagId by string nay chua?
                    var tag1 = db.Tags.FirstOrDefault(t => t.TagName == tagString);
                    if (tag1 != null)
                    {
                        tagId = tag1.TagId;
                        //UsedCount++
                        tag1.UsedCount++;
                        db.SaveChanges();
                    }
                    else
                    {
                        //insert tagName
                        var tag2 = new Tags() { TagName = tagString, UsedCount = 1 };
                        db.Tags.AddObject(tag2);

                        if (db.SaveChanges() > 0)
                        {
                            tagId = tag2.TagId;
                        }

                    }
                    if (tagId > 0)
                        db.TagsSupplier.AddObject(new TagsSupplier() { SupplierId = supplierid, TagString = tagString, TagId = tagId });
                }
                db.SaveChanges();
            }
            catch (Exception)
            {
                return rs;
                //throw;
            }
            return rs;
        }

        public Tags GetTagByText(string keyTag)
        {
            try
            {
                using (TopLinkEntities db = new TopLinkEntities())
                {
                    return db.Tags.Where(e => e.TagName.ToLower() == keyTag.ToLower()).FirstOrDefault();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
